local blaze = require "blaze"
local game = require "club.threecard.hbsl"
local GameObjectExtension = CS.Blaze.GameObjectExtension
local Vector3 = CS.UnityEngine.Vector3
local const = game.util.const

local SPACING = -100

local M = game.ui.element()

M.store_watchers = {
    gameplay = {
        ["$"] = blaze.noop,

        ["抢红包"] = function(self, store, state, player, init)
            self:do_grab(player)
        end,

        ["阶段变更"] = function(self, store, state, args, init)
            if state.phase_id == const.PHASE_IDLE then
                self:clear()
            end
        end,

        ["红包结果"] = function(self, store, state, args, init)
            local hits = args.hits

            local is_hit = function(id)
                for _, v in  ipairs(hits) do
                    if id == v then
                        return true
                    end
                end

                return false
            end

            local max_amount = args.max_amount

            local records = args.records
            for _, t in ipairs(records) do
                if self.items[t.player_id] then
                    self.items[t.player_id]:on_reveal(t.amount, is_hit(t.player_id), max_amount)
                end
            end
        end,

        ["结算"] = function(self, store, state, args, init)
            for _, t in ipairs(args) do
                if self.items[t.player_id] then
                    self.items[t.player_id]:on_settle(t.payout)
                end
            end
        end
    }
}

function M:ctor(packet_dispatcher)
    self.res = "club.threecard.hbsl/ui/ingame.b:participant_shower"
    self.count = 0
    self.left_count = 0
    self.right_count = 0
    self.items = {}
    self.packet_dispatcher = packet_dispatcher
end

function M:on_injected(bind)
    local participant = game.require "ui.ingame.participant"
    self.left_pool = blaze.pool.create(participant, { prefab = self.temp_left, parent = self.left_root })
    self.right_pool = blaze.pool.create(participant, { prefab = self.temp_right, parent = self.right_root })
end

function M:clear()
    self.left_pool:recycle_all()
    self.right_pool:recycle_all()
    self.items = {}
    self.count = 0
    self.left_count = 0
    self.right_count = 0
end

function M:do_grab(player)
    self.count = self.count + 1
    local obj = nil

    local index
    local dis_x
    local p
    if self.count % 2 == 0 then
        index = self.right_count
        self.right_count = self.right_count + 1
        p = self.right_pool:get()
        dis_x = 244.16
    else
        index = self.left_count
        self.left_count = self.left_count + 1
        p = self.left_pool:get()
        dis_x = -244.16
    end

    p:set_player(player)
    p:appear(dis_x, index * SPACING, -dis_x)
    self.packet_dispatcher:dispatch(p.transform)
    local old = self.items[player.id]
    if old then
        old.__pool:put(old)
    end
    self.items[player.id] = p
end

function M:on_release()
    self:clear()
    self.left_pool:release()
    self.right_pool:release()
end

return M